﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataAccess;
using System.Diagnostics;
using SPIWareMVVM.ViewModels;
using Microsoft.Windows.Controls.Ribbon;
using System.Configuration;

namespace SPIWareMVVM.Views
{
    /// <summary>
    /// Interaktionslogik für Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public const bool TryTests = false;
        ApplicationViewModel applicationViewModel { get; set; }

        public Window1()
        {
            //// Get the current identity.
            //System.Security.Principal.WindowsIdentity MyIdent = System.Security.Principal.WindowsIdentity.GetCurrent();

            //// Create a principal.
            //WindowsPrincipal MyPrincipal = new WindowsPrincipal(MyIdent);

            InitializeComponent();

            applicationViewModel = (ApplicationViewModel)((ObjectDataProvider)this.Resources["applicationVM"]).ObjectInstance;
            if (this.cboRunDates.Items.Count > 0) this.cboRunDates.SelectedIndex = 0;
            HideLists();

            if (TryTests) CreatePart();
        }
        public void HideLists()
        {
            customersList.dpAddressEdit.Visibility = System.Windows.Visibility.Collapsed;
            customersList.dpCustomerEdit.Visibility = System.Windows.Visibility.Collapsed;
            if (ApplicationData.SelectedCustomer == null) this.customersList.Visibility = Visibility.Collapsed;
            //this.customerEdit.Visibility = Visibility.Collapsed;
            this.commissionsList.Visibility = Visibility.Collapsed;
            this.deliveryList.Visibility = Visibility.Collapsed;
            if (ApplicationData.SelectedMaterial == null) this.materialsList.Visibility = Visibility.Collapsed;
            if (ApplicationData.SelectedMachinePress == null) this.pressMachinesList.Visibility = Visibility.Collapsed;
            this.productionsList.Visibility = Visibility.Collapsed;
            if (ApplicationData.SelectedWorkOrder == null) this.workOrdersList.Visibility = Visibility.Collapsed;
            this.workOrdersList.dpWorkOrderEdit.Visibility = Visibility.Collapsed;

            this.workOrderReport.Visibility = Visibility.Collapsed;
        }

        private void btnActions_Click(object sender, RoutedEventArgs e)
        {
            VisualStateManager.GoToState(this, "CanvasShown", true);
        }

        private void RunDateSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                DateTime endDate = ((DateTime)e.AddedItems[0]).AddDays(1);
                ApplicationData.SelectedRunDate = endDate.AddDays(-1);
                //this.productionsList.RaiseEvent(new RoutedEventArgs())

                this.productionsList.lbProduction.ItemsSource = ApplicationData.GetFilteredProduction(endDate);
                if (this.productionsList.lbProduction.Items.Count > 0) this.productionsList.lbProduction.SelectedIndex = 0;

                if (this.productionsList.Visibility == Visibility.Collapsed) this.productionsList.Visibility = Visibility.Visible;
            }
        }

        private void CreateMaterial()
        {
            MaterialType m = new MaterialType();
            m.CostPerLB = 1.2;
            m.MaterialType1 = "PD702 PP Natural";
            m.MaterialVendor = "Ashland";

            applicationViewModel.SaveCommand.Execute(m);
        }
        private void CreatePart()
        {
            Part p = new Part();
            MaterialType m = ApplicationData.ocMaterialTypes.Where(x => x.MaterialType1 == "PD702 PP Natural").FirstOrDefault();
            if (m != null)
            {
                p.MaterialId = m.MaterialId;

                p.PartNumber = "8003";
                p.Description = "Hinge Corner Piece";
                p.Price = 0.748;
                p.PressSize = 150;
                p.PressRate =45;
                p.PressOpCost =1.5;
                p.OpCostPerPart =.014;
                p.PartWeight =.08;
                p.PartsPerShot =1;
                p.ShotWeight =.12;
                p.TargetCycleTime =30;
                p.PartsPerHour = 120;
                p.AverageCycleTime_AS = 33;
                p.PartsPerHour_AS = 109;
                p.DyeMixRatio = 0;
                p.NaturalResin = .098;
                p.DyeNumber = "Natural";
                p.DyeCostPerPart = 0;
                p.DyeCostPerLB = 0;
                p.ResinDyePricePerPart = .096;
                p.MoldSetupTime = 1;
                p.MoldSetupCost = 20;
                p.StartupScrap = 5;
                p.StartupScrapPrice = 6;
                p.BoxSize = "16x16x16";
                p.PartsPerBox = 400;
                p.BoxPrice = .69;
                p.BoxesPerPallet = 18;
                p.PartsPerPallet = 7200;
                p.PricePerBox = .05;

                applicationViewModel.SaveCommand.Execute(p);
            }
        }
    }
}
